import config from '../../../config/index.js'
import util from '../../../utils/util.js'

import {
    login,
    home,
} from '../../../services/index.js'

var app = getApp();
Page({
  data: {
    address: {
      id:0,
      province_id: 0,
      city_id: 0,
      district_id: 0,
      address: '',
      full_region: '',
      name: '',
      mobile: '',
      is_default: 0
    },
    addressId: 0,
    openSelectRegion: false,
    selectRegionList: [
      { id: 0, name: '省份', parent_id: 1, type: 1 },
      { id: 0, name: '城市', parent_id: 1, type: 2 },
      { id: 0, name: '区县', parent_id: 1, type: 3 }
    ],
    regionType: 1,
    regionList: [],
    selectRegionDone: false,
    region:[],
    count:1,
  },

  chooseAddress() {
    let self=this
    wx.chooseAddress({
      success: (res) => {
        console.log(res)
        self.setData({
          'address.name':res.userName,
          'address.mobile':res.telNumber,
          'address.address':res.detailInfo,
          'region[0]':res.provinceName,
          'region[1]':res.cityName,
          'region[2]':res.countyName,
          addressInfo: res
        },()=>{})
      },
      fail: function(err) {
        console.log(err)
      }
    })
  
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options)
    if (options.id) {
      this.setData({
        id: options.id
      },()=>{
        this.GetProductDetail()
      });
    }


  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  GetProductDetail(){
      console.log('GetProductDetail')
      home.GetProductDetail({id:this.data.id}).then(res => {
          if(res && res.row && res.row.bsItem){
              this.setData({
                  goods:res.row.bsItem,
              },()=>{

              })
          }
      })
  },

  showModal(text){
    wx.showModal({
      title: '提示',
      content: text,
      showCancel:false,
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  saveAddress(){
    console.log(this.data.address)
    let address = this.data.address;

    if (address.name == '') {
      util.showModal('请输入姓名');
      return false;
    }

    if (address.mobile == '') {
      util.showModal('请输入手机号码');
      return false;
    }


    if (!this.data.region ||!this.data.region[0] ||!this.data.region[0].length) {
      util.showModal('省份、城市、区县');
      return false;
    }

    if (address.address == '') {
      util.showModal('请输入工厂地址');
      return false;
    }

    this.cloudPay()
console.log(this.data.address)

  },

  cutNumber: function (e) {

    let count = (this.data.count - 1 > 1) ? this.data.count - 1 : 1;
    // cartItem.count = count;
    this.setData({
      count: count
    },()=>{
    });
    // this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  },
  addNumber: function (e) {
    console.log('addNumber')
    // let fweight = this.goods.fweight||10
    let fweight = 10
    let count = this.data.count + 1;
    count = count>fweight?fweight:count;
    this.setData({
      count: count
    },()=>{
    });
    // this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);

  },
  cloudPay: function(e) {

      wx.showLoading({
          title: '加载中...',
      })
      let that = this;
      var uuid = this.uuid(16, 16) //调用自己的uuid函数
      var body = this.data.goods.fname

      wx.cloud.callFunction({
          name: "pay",
          data: {
              body: body,
              orderid: "" + uuid,
              money: ~~(this.data.goods.fprice*this.data.count*100), //支付金额
              nonceStr: this.uuid(32, 32) //调用自己的uuid函数
          },
          success(res) {
              wx.hideLoading({
                  complete: (res) => {},
              })
              console.log("提交成功", res.result)
              //创建自己的未支付订单
              // 。。。

              that.pay(res.result)
          },
          fail(res) {
              wx.hideLoading({
                  complete: (res) => {},
              })
              console.log("提交失败", res)
          }
      })
  },

  //实现小程序支付
  pay(payData) {
      var that = this;
      const payment = payData.payment //这里注意，上一个函数的result中直接整合了这里要用的参数，直接展开即可使用
      wx.requestPayment({
          ...payment, //。。。是展开变量的语法 
          success(res) {
              console.log('pay success', res)
              //跳转到支付成功页面
          },
          fail(res) {
              console.error('pay fail', res)
              //跳转到支付失败页面
          }
      })
  },
  uuid(len, timestamp) {
      len = len || 32
      var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
      /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
      var maxPos = $chars.length
      var pwd = ''
      for (var i = 0; i < len; i++) {
          pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
      }
      return pwd
  },
  bindinputName(e) {
    this.setData({
      'address.name': e.detail.value
    });    
  },
  bindinputMobile(e) {
    this.setData({
    'address.mobile' :e.detail.value
    });
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bindinputAddress (e){
    this.setData({
    'address.address': e.detail.value
    });
  },
  bindinputEmail (e){
    this.setData({
    'address.Email': e.detail.value
    });
  },
  bindinputRemark (e){
    this.setData({
    'address.remark': e.detail.value
    });
  },
  bindIsDefault(){},

})